package com.tengfei.ui_widget;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class SpinnerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        //数据（Data）<-->数据适配器（Adapter）<-->界面（Spinner）
        getSupportActionBar().setTitle("Spinner");
        //region 学期下拉框：Spinner加载数据静态数据
        Spinner spSemester = findViewById(R.id.sp_semester);
        spSemester.setSelection(0, true);
        spSemester.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                if(position == 0) return;
                Toast.makeText(SpinnerActivity.this,
                        adapterView.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //endregion

        //region 学院下拉框：Spinner加载动态数据，使用系统预定义下拉列表项视图
        String[] school = {"-==选择学院==-", "计算机学院", "理学院", "外语学院"};
        Spinner spschool = findViewById(R.id.sp_school);
        spschool.setSelection(0,true);
        ArrayAdapter<String> schoolAdapter =
                new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, school);
        spschool.setAdapter(schoolAdapter);
        spschool.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                if(position == 0) return;
                Toast.makeText(SpinnerActivity.this,
                        adapterView.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //endregion

        //region 年级下拉框，Spinner通过ArrayAdapter加载动态数据，使用自定义下拉列表项视图
        String[] grades = {"-==选择年级==-", "大一", "大二", "大三", "大四"};
        Spinner spGrade = findViewById(R.id.sp_grade);
        spGrade.setSelection(0,true);
        ArrayAdapter<String> gradeAdapter =
                new ArrayAdapter<>(this, R.layout.my_spinner_item, grades);
        gradeAdapter.setDropDownViewResource(R.layout.my_spinner_item_dropdown);
        spGrade.setAdapter(gradeAdapter);
        spGrade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                if(position == 0) return;
                Toast.makeText(SpinnerActivity.this,
                        adapterView.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //endregion
    }
}